package com.home766.controller.shop;

import com.allinpay.ets.client.RequestOrder;
import com.allinpay.ets.client.SecurityUtil;
import com.home766.dto.PaymentDTO;
import com.home766.util.PropertyUtil;
import net.biz.util.ParamsUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/returnpayment")
public class PaymentTLController {

	
	
	/**
	 * 返回页面
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/returnpage")
	public String returnPage(PaymentDTO dto){
		if(null!=dto){
			System.out.println("页面接收接收："+dto.toString());
			System.out.println("///////////////////////////////////////");
			System.out.println("///////////////////////////////////////");
			System.out.println("///////////////////////////////////////");
			System.out.println("///////////////////////////////////////");
			System.out.println("///////////////////////////////////////");
		}
		return null;
	}
	/**
	 * 服务器逻辑处理
	 * @return
	 */
	@RequestMapping(value="/returnserver")
	public String returnServer(PaymentDTO dto){
		if(null!=dto){
			System.out.println("服务器接收："+dto.toString());
			System.out.println("///////////////////////////////////////");
			System.out.println("///////////////////////////////////////");
			System.out.println("///////////////////////////////////////");
			System.out.println("///////////////////////////////////////");
			System.out.println("///////////////////////////////////////");
		}
		return null;
	}
	
	
	
	
	@RequestMapping(value="/tlPost")
	public String post(HttpServletRequest request, ModelMap model){
		String serverUrl =PropertyUtil.getValue("serverUrl");
		String key=PropertyUtil.getValue("key");
		String version=PropertyUtil.getValue("version");
		String language=PropertyUtil.getValue("language");
		String inputCharset= PropertyUtil.getValue("inputCharset");
		String merchantId=PropertyUtil.getValue("merchantId");
		String pickupUrl=PropertyUtil.getValue("pickupUrl");
		String receiveUrl=PropertyUtil.getValue("receiveUrl");
		String payType=PropertyUtil.getValue("payType");
		String signType=PropertyUtil.getValue("signType");
		String orderNo=PropertyUtil.getValue("orderNo");
		String orderAmount=PropertyUtil.getValue("orderAmount");
		String orderDatetime=PropertyUtil.getValue("orderDatetime");
		String orderCurrency=PropertyUtil.getValue("orderCurrency");
		String orderExpireDatetime=PropertyUtil.getValue("orderExpireDatetime");
		String payerTelephone=PropertyUtil.getValue("payerTelephone");
		String payerEmail=PropertyUtil.getValue("payerEmail");
		String payerName=PropertyUtil.getValue("payerName");
		String payerIDCard=PropertyUtil.getValue("payerIDCard");
		String pid=PropertyUtil.getValue("pid");
		String productName=PropertyUtil.getValue("productName");
		String productId=PropertyUtil.getValue("productId");
		String productNum=PropertyUtil.getValue("productNum");
		String productPrice=PropertyUtil.getValue("productPrice");
		String productDesc=PropertyUtil.getValue("productDesc");
		String ext1=PropertyUtil.getValue("ext1");
		String ext2=PropertyUtil.getValue("ext2");
		String extTL=PropertyUtil.getValue("extTL");//通联商户拓展业务字段，在v2.2.0版本之后才使用到的，用于开通分账等业务
		String issuerId=PropertyUtil.getValue("issuerId");
		String pan=PropertyUtil.getValue("pan");
		String tradeNature = PropertyUtil.getValue("tradeNature");
		String sign="";
		
		//若直连telpshx渠道，payerTelephone、payerName、payerIDCard、pan四个字段不可为空
		//其中payerIDCard、pan需使用公钥加密（PKCS1格式）后进行Base64编码
		if(null!=payerIDCard&&!"".equals(payerIDCard)){
			try{
				//payerIDCard = SecurityUtil.encryptByPublicKey("C:\\TLCert.cer", payerIDCard);   /opt/conf/TLCert.cer
				payerIDCard = SecurityUtil.encryptByPublicKey(ParamsUtil.TLCERT_PATH, payerIDCard);
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		if(null!=pan&&!"".equals(pan)){
			try{
				pan = SecurityUtil.encryptByPublicKey(ParamsUtil.TLCERT_PATH, pan);
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		
		//构造订单请求对象，生成signMsg。
		RequestOrder requestOrder=new RequestOrder();
//		com.allinpay.ets.client.RequestOrder requestOrder = new com.allinpay.ets.client.RequestOrder();
		if(null!=inputCharset&&!"".equals(inputCharset)){
			requestOrder.setInputCharset(Integer.parseInt(inputCharset));
		}
		requestOrder.setPickupUrl(pickupUrl);
		requestOrder.setReceiveUrl(receiveUrl);
		requestOrder.setVersion(version);
		if(null!=language&&!"".equals(language)){
			requestOrder.setLanguage(Integer.parseInt(language));
		}
		requestOrder.setSignType(Integer.parseInt(signType));
		requestOrder.setPayType(Integer.parseInt(payType));
		requestOrder.setIssuerId(issuerId);
		requestOrder.setMerchantId(merchantId);
		requestOrder.setPayerName(payerName);
		requestOrder.setPayerEmail(payerEmail);
		requestOrder.setPayerTelephone(payerTelephone);
		requestOrder.setPayerIDCard(payerIDCard);
		requestOrder.setPid(pid);
		requestOrder.setOrderNo(orderNo);
		requestOrder.setOrderAmount(Long.parseLong(orderAmount));
		requestOrder.setOrderCurrency(orderCurrency);
		requestOrder.setOrderDatetime(orderDatetime);
		requestOrder.setOrderExpireDatetime(orderExpireDatetime);
		requestOrder.setProductName(productName);
		if(null!=productPrice&&!"".equals(productPrice)){
			requestOrder.setProductPrice(Long.parseLong(productPrice));
		}
		if(null!=productNum&&!"".equals(productNum)){
			requestOrder.setProductNum(Integer.parseInt(productNum));
		}	
		requestOrder.setProductId(productId);
		requestOrder.setProductDesc(productDesc);
		requestOrder.setExt1(ext1);
		requestOrder.setExt2(ext2);
		requestOrder.setExtTL(extTL);//通联商户拓展业务字段，在v2.2.0版本之后才使用到的，用于开通分账等业务
		requestOrder.setPan(pan);
		requestOrder.setTradeNature(tradeNature);
		requestOrder.setKey(key); //key为MD5密钥，密钥是在通联支付网关会员服务网站上设置。

		String strSrcMsg = requestOrder.getSrc(); // 此方法用于debug，测试通过后可注释。
		String strSignMsg = requestOrder.doSign(); // 签名，设为signMsg字段值。
		model.addAttribute("strSrcMsg", strSrcMsg);
		model.addAttribute("strSignMsg", strSignMsg);
		model.addAttribute("sign", sign);
		model.addAttribute("serverUrl", serverUrl);
		model.addAttribute("key", key);
		model.addAttribute("version", version);
		model.addAttribute("language", language);
		model.addAttribute("inputCharset", inputCharset);
		model.addAttribute("merchantId", merchantId);
		model.addAttribute("pickupUrl", pickupUrl);
		model.addAttribute("receiveUrl", receiveUrl);
		model.addAttribute("payType", payType);
		model.addAttribute("signType", signType);
		model.addAttribute("orderNo", orderNo);
		model.addAttribute("orderAmount", orderAmount);
		model.addAttribute("orderDatetime", orderDatetime);
		model.addAttribute("orderCurrency", orderCurrency);
		model.addAttribute("orderExpireDatetime", orderExpireDatetime);
		model.addAttribute("payerTelephone", payerTelephone);
		model.addAttribute("payerEmail", payerEmail);
		model.addAttribute("payerName", payerName);
		model.addAttribute("payerIDCard", payerIDCard);
		model.addAttribute("pid", pid);
		model.addAttribute("productName", productName);
		model.addAttribute("productId", productId);
		model.addAttribute("productNum", productNum);
		model.addAttribute("productPrice", productPrice);
		model.addAttribute("productDesc", productDesc);
		model.addAttribute("ext1", ext1);
		model.addAttribute("ext2", ext2);
		model.addAttribute("extTL", extTL);
		model.addAttribute("issuerId", issuerId);
		model.addAttribute("pan", pan);
		model.addAttribute("tradeNature", tradeNature);
		return "/shop/tl/post";
	}
	
}
